/**
 ******************************************************************************
 * @file   drv_f4_timer_int.c
 * @brief  定时器中断功能
 * @author Evan
 * @version V1.0.0
 * @date 2023/02/20
 ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "drv_f4_timer_int.h"
#include "main_task.h"
#include "dev_ad7193_press.h"	
#include "system.h"
#include "FreeRTOS.h"

#include "task.h"
#include "semphr.h"
/* Private defines -----------------------------------------------------------*/	 

extern RunStateMachine_e rsm;
uint32_t FreeRTOSRunTicks = 0;

//对于stm32f407 168M主频，因为系统初始化SystemInit函数里初始化APB1总线时钟为4分频即42M，所以TIM2~TIM7、TIM12~TIM14的时钟为APB1的时钟的两倍即84M；
//APB2总线时钟为2分频即84M，TIM1、TIM8~TIM11的时钟为APB2时钟的两倍即168M。
//Tout = ((arr+1)*(psc+1))/Tclk


//通用定时器3中断初始化
//arr：自动重装值。
//psc：时钟预分频数
//定时器溢出时间计算方法:Tout=((arr+1)*(psc+1))/Ft us.
//Ft=定时器工作频率,单位:Mhz
//这里使用的是定时器3!
void TIM3_Int_Init(u16 arr)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  ///使能TIM3时钟
	
	TIM_TimeBaseInitStructure.TIM_Period = arr; 	//自动重装载值
	TIM_TimeBaseInitStructure.TIM_Prescaler=8400;  //定时器时钟84M，分频系数8400，所以84M/8400=10Khz的计数频率
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//初始化TIM3
	
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //允许定时器3更新中断
	TIM_Cmd(TIM3,ENABLE); //使能定时器3
	
	NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=configMAX_SYSCALL_INTERRUPT_PRIORITY; //抢占优先级1
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00; //子优先级0
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);

}


uint8_t CPU_RunInfo[500];		//保存任务运行时间信息
void TIM3_IRQHandler(void)
{
    TIM_ClearITPendingBit(TIM3,TIM_IT_Update);  //清除中断标志位
    FreeRTOSRunTicks++;
}
